package cn.good.user.service;


import cn.good.pojo.User;
import cn.good.service.SmsCodeService;
import cn.good.service.TokenService;
import cn.good.service.UserService;
import cn.good.utils.Constant;
import cn.good.utils.MD5;
import cn.good.utils.RedisUtil;
import cn.good.vo.Phone;
import cn.good.vo.UserVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

@Service
public class QGUserServiceImpl implements QGUserService {

    @Reference
    private UserService userService;
    @Reference
    private TokenService tokenService;
    @Reference
    private SmsCodeService smsCodeService;
    @Resource
    private RedisUtil redisUtils;


    /**
     * 用户登录
     * @param userCode
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public String getUserByUserCode(String userCode,String password) throws Exception {
        if(userCode == null || userCode.equals("")){
            throw new Exception("userCode参数设置错误!");
        }
        //查询数据库数据----------
        User user = null;
        try {
            user = userService.getUserByUserCode(userCode);
        } catch (Exception e) {
            throw new Exception("查询失败！");
        }
        if(user==null){
            throw new Exception("该用户不存在!");
        }
        if(!MD5.getMd5(password,32).equals(user.getUserPassword())){
            throw new Exception("密码有误！？");
        }
        String token = tokenService.createToken(user);
       return token;
    }

    /**
     * 注销
     * @param token
     * @throws Exception
     */
    @Override
    public void logOut(String token) throws Exception {
        tokenService.logOut(token);
    }

    /**
     * 用户是否存在
     * @param userCode
     * @return
     * @throws Exception
     */
    @Override
    public boolean userExist(String userCode) throws Exception {
        User user = userService.getUserByUserCode(userCode);
        if(user==null) {
            return true;
        }else {
            throw new Exception("用户名已存在！");
        }
    }

    /**
     * 保存注册用户
     * @param userVO
     * @throws Exception
     */
    @Override
    public void saveUser(UserVO userVO) throws Exception {
        if(userVO.getUserName()==null||userVO.getUserPassword()==null||userVO.getUserName().equals("")||userVO.getUserPassword().equals("")){
            throw new Exception("参数错误");
        }
        User user = new User();
        user.setUserCode(userVO.getUserCode());
        user.setUserPassword(MD5.getMd5(userVO.getUserPassword(),32));
        if(userVO.getUserName()!=null)
            user.setUserName(userVO.getUserName());
        userService.saveUser(user);
    }

    /**
     * 保存注册用户
     * @param user
     * @throws Exception
     */
    @JmsListener(destination = Constant.MQ_PHONE)
    public void saveUser(String user) throws Exception {
        UserVO userVO = JSON.parseObject(user,UserVO.class);
        if(userVO.getUserName()==null||userVO.getUserPassword()==null||userVO.getUserName().equals("")||userVO.getUserPassword().equals("")){
            throw new Exception("参数错误");
        }
        User user1 = new User();
        user1.setUserCode(userVO.getUserCode());
        user1.setUserPassword(MD5.getMd5(userVO.getUserPassword(),32));
        if(userVO.getUserName()!=null)
            user1.setUserName(userVO.getUserName());
        userService.saveUser(user1);
    }


    /**
     * 发送短信
     * @param userVO
     */
    @JmsListener(destination = Constant.MQ_PHONE_JSM)
    public void sendSmsCode(String userVO)throws Exception{
        UserVO user = JSON.parseObject(userVO,UserVO.class);
        String phone = user.getUserCode();
       Integer code = MD5.getRandomCode();
        Phone value = new Phone();
        value.setPhone(phone);
        value.setCode(code.toString());
        value.setCreateTime(new Date().getTime());
       redisUtils.set(Constant.PHONE_PREFIX+phone,new Long(Constant.REGISTER_TIME).intValue(),JSON.toJSONString(value));
       smsCodeService.smsCodeSend(phone,code.toString());
    }

    /**
     * 手机用户激活
     * @param userCode
     * @param value
     * @return
     * @throws Exception
     */
    @Override
    public boolean phoneActivate(String userCode, String value) throws Exception {
        String phone = (String) redisUtils.get(Constant.PHONE_PREFIX+userCode);
        if(phone != null){
            Phone phone1 = JSON.parseObject(phone,Phone.class);
            if(value.equals(phone1.getCode())){
                //激活用户
                redisUtils.delete(Constant.PHONE_PREFIX+userCode);
                return true;
            }
        }
        return false;
    }
}
